home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Papers / Garrison / Code / SchmoozingExamples / Connection.m < prev    next >
Encoding:
Text File  |  2001-06-23  |  2.7 KB  |  95 lines

  1. //
  2. //  Connection.m
  3. //  SchoomzingExamples
  4. //
  5. //  Created by garrison on Fri Apr 20 2001.
  6. //  Copyright (c) 2001 Standard Orbit Software, LLC. All rights reserved.
  7. //
  8. //  Permission is granted to use this code for any purpose, at your own risk.
  9. //  No warranties are expressed or implied.
  10.  
  11. #import <OmniNetworking/OmniNetworking.h>
  12. #import "Connection.h"
  13.  
  14. @implementation Connection
  15.  
  16. - (void) processConnection
  17. {
  18.     // processing of the connection in this server is pretty minimal.
  19.     // Read a line of input from the client and echo it back to the
  20.     // client.  If the line is "STOP", then set the server up to exit.
  21.     
  22.     NSAutoreleasePool *pool = nil;
  23.     ONSocketStream *stream = nil;
  24.     NSString *inFromClient = nil;
  25.     
  26.     pool = [[NSAutoreleasePool alloc] init];
  27.     // Create an autorelease pool to contain memory used in this scope.
  28.  
  29.     stream = [ONSocketStream streamWithSocket: mySocket];
  30.     // Put an ONSocketStream object on the mySocket to simplify reading.
  31.  
  32.     do {
  33.  
  34.         NS_DURING {
  35.             inFromClient = [stream readLine];
  36.             // Read a line from the client.  Will read up to a NL or CRNL.
  37.  
  38.             [stream writeFormat:@"%@: %@ received connection from %@\r\n", 
  39.                 [NSCalendarDate calendarDate],  [ONHost localHostname],
  40.                 [[mySocket remoteAddressHost] hostname]];
  41.             // Write a formatted header back to the client.
  42.         
  43.             [stream writeFormat:@"%@\r\n", [inFromClient uppercaseString]];
  44.             // Echo the input line back to the client.
  45.         }
  46.         NS_HANDLER {
  47.             NSLog(@"While processing connection, exception %@ was raised",
  48.                 [localException name]);
  49.             break;
  50.         }
  51.         NS_ENDHANDLER
  52.                   
  53.  
  54.         // In this example, we're done talking to the client so we'll initiate
  55.         // the closing of the connection with abortSocket:.  In a real app, the
  56.         // application-level protocol would probably dictate when the connection
  57.         // with the client should end.
  58.         [mySocket abortSocket];
  59.         break;
  60.     
  61.  
  62.     } while(1);
  63.     // Loop forever processing the connection with the client.  A raised exception or
  64.     // the receipt of an application-level protocol command to close the connection
  65.     // should cause a break out of the loop.        
  66.  
  67.     [pool release];
  68.  
  69. }
  70.  
  71.  
  72.  
  73. - initWithConnectedSocket: (ONTCPSocket*) aSocket
  74. {
  75.     self = [super init];
  76.     if ( self ) 
  77.     {
  78.  
  79.         if ( [aSocket isConnected] )
  80.             mySocket = [aSocket retain];
  81.         else
  82.             return nil;
  83.         // We should initialized with connected socket, so if not, return
  84.     }
  85.     
  86.     return self;
  87. }
  88.  
  89. - (void) dealloc
  90. {
  91.     [mySocket release];
  92.     [super dealloc];
  93. }
  94. @end
  95.